//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.CustomerManagement;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class CustomerShippingAddressControl: BaseNopFrontendUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (NopContext.Current.User == null)
            {
                string loginURL = SEOHelper.GetLoginPageUrl(true);
                Response.Redirect(loginURL);
            }

            if (!Page.IsPostBack)
            {
                ShippingAddressEditCtrl.ValidationGroup = btnSave.ValidationGroup;
                BindShippingAddress();
                DoDefaultView();
            }
        }

        public void BindShippingAddress()
        {
            var shippingAddresses = NopContext.Current.User.ShippingAddresses;
            rptrShippingAddress.DataSource = shippingAddresses;
            rptrShippingAddress.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DoNewView();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
          if (Page.IsValid)
          {
            var oldAddress = this.CustomerService.GetAddressById(this.ShippingAddressEditCtrl.AddressId);
            var inputedAddress = ShippingAddressEditCtrl.ShippingAddress;
            if (oldAddress != null)
            {
              oldAddress.FirstName = inputedAddress.FirstName;
              oldAddress.LastName = inputedAddress.LastName;
              oldAddress.PhoneNumber = inputedAddress.PhoneNumber;
              oldAddress.Email = inputedAddress.Email;
              oldAddress.FaxNumber = inputedAddress.FaxNumber;
              oldAddress.Company = inputedAddress.Company;
              oldAddress.Address1 = inputedAddress.Address1;
              oldAddress.Address2 = inputedAddress.Address2;
              oldAddress.City = inputedAddress.City;
              oldAddress.StateProvinceId = inputedAddress.StateProvinceId;
              oldAddress.ZipPostalCode = inputedAddress.ZipPostalCode;
              oldAddress.CountryId = inputedAddress.CountryId;
              oldAddress.UpdatedOn = DateTime.UtcNow;

              oldAddress.INN = inputedAddress.INN;
              oldAddress.KPP = inputedAddress.KPP;
              oldAddress.OGRN = inputedAddress.OGRN;
              oldAddress.BankName = inputedAddress.BankName;
              oldAddress.BankAddress = inputedAddress.BankAddress;
              oldAddress.BIK = inputedAddress.BIK;
              oldAddress.KorrAcc = inputedAddress.KorrAcc;
              oldAddress.RS = inputedAddress.RS;

              this.CustomerService.UpdateAddress(oldAddress);
            }
            else
            {
              inputedAddress.CustomerId = NopContext.Current.User.CustomerId;
              inputedAddress.CreatedOn = DateTime.UtcNow;
              inputedAddress.UpdatedOn = DateTime.UtcNow;
              this.CustomerService.InsertAddress(inputedAddress);

              if (NopContext.Current.User.ShippingAddressId == 0)
              {
                NopContext.Current.User.ShippingAddressId = inputedAddress.AddressId;
                CustomerService.UpdateCustomer(NopContext.Current.User);
              }
            }

            DoDefaultView();
            BindShippingAddress();
            //Response.Redirect(SEOHelper.GetMyShippingAddressUrl());
          }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
          DoDefaultView();
        }

        protected void rptrShippingAddress_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
          {
            var address = e.Item.DataItem as Address;

            var rdShippingOption = (RadioButton)e.Item.FindControl("ShippingAddressDisplayCtrl").FindControl("rdShippingOption");
            var hfAddressId = (HiddenField)e.Item.FindControl("ShippingAddressDisplayCtrl").FindControl("hfAddressId");
            var imgFlag =  (Image)e.Item.FindControl("ShippingAddressDisplayCtrl").FindControl("imgFlag");
            if (address.AddressId == NopContext.Current.User.ShippingAddressId)
              rdShippingOption.Checked = true;
            else
              imgFlag.Visible = false;
          }
       }

        public void SaveDefaultAddress()
        {
          Customer cust = NopContext.Current.User;
          cust.ShippingAddressId = DefaultAddress.AddressId;
          CustomerService.UpdateCustomer(cust);
          BindShippingAddress();
        }

        public Address DefaultAddress
        {
          get
          {
            Address result = null;

            foreach (RepeaterItem item in this.rptrShippingAddress.Items)
            {
              var rdShippingOption = (RadioButton)item.FindControl("ShippingAddressDisplayCtrl").FindControl("rdShippingOption");
              var hfAddressId = (HiddenField)item.FindControl("ShippingAddressDisplayCtrl").FindControl("hfAddressId");

              if (rdShippingOption.Checked)
              {
                int addressId = Convert.ToInt32(hfAddressId.Value);
                result = CustomerService.GetAddressById(addressId);
                break;
              }
            }

            return result;
          }
        }

        public void DoDefaultView()
        {
          phAddButton.Visible = false;
          phSaveButton.Visible = false;
          phCard.Visible = false;

          phAddButton.Visible = true;
        }

        public void DoNewView()
        {
          phAddButton.Visible = false;
          phSaveButton.Visible = false;
          phCard.Visible = false;

          phSaveButton.Visible = true;
          phCard.Visible = true;

          ShippingAddressEditCtrl.IsNew = true;
          ShippingAddressEditCtrl.DoNew();
        }

        public void DoEditView()
        {

          phAddButton.Visible = false;
          phSaveButton.Visible = false;
          phCard.Visible = false;

          phSaveButton.Visible = true;
          phCard.Visible = true;

          var address = this.CustomerService.GetAddressById(this.AddressId);
          if (address != null)
          {
            ShippingAddressEditCtrl.IsNew = false;
            ShippingAddressEditCtrl.ShippingAddress = address;
          }
        }

        [DefaultValue(-1)]
        public int AddressId
        {
          get
          {
            int result = -1;
            object obj2 = this.ViewState["AddressId"];
            if (obj2 != null)
              result = (int)obj2;
            return result;
          }
          set
          {
            this.ViewState["AddressId"] = value;
          }
        }
    }
}